package com.midea.ai.air.ui.more;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.view.MyTitleBar;

/**
 * Created by hww on 2016/4/20.
 * 新建联动 Activity
 */
public class MoreSettingNewBrainpower extends BaseActivity implements View.OnClickListener {

    //选择房间相关控件
    private TextView mTv_one, mTv_room, mTv_room_ll, mTv_room_ll_no_select;
    private ImageView mIv_room_ll;
    private RelativeLayout mRl_room;
    //启动条件相关控件
    private TextView mTv_two, mTv_condition, mTv_condition_ll, mTv_condition_ll_no_select;
    private ImageView mIv_condition_ll;
    private RelativeLayout mRl_condition;
    //执行任务相关控件
    private TextView mTv_three, mTv_task, mTv_task_ll, mTv_task_ll_no_select;
    private ImageView mIv_task_ll;
    private RelativeLayout mRl_task;

    public static final int REQUEST_ONE = 1;
    public static final int REQUEST_TWO = 2;
    public static final int REQUEST_THREE = 3;

    public static final int RESULT_SUCCESS = 0;
    public static final int RESULT_FAILED = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_moresettingnewbrainpower);

        init();
    }

    MyTitleBar myTitleBar;
    private TextView actionbar_left_tv, actionbar_right_tv;
    private RelativeLayout bar_Rl;

    private void init() {
        bar_Rl = (RelativeLayout) findViewById(R.id.bar_parent_rl);
        ViewGroup.LayoutParams LP = bar_Rl.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        bar_Rl.setLayoutParams(LP);

        myTitleBar = new MyTitleBar(this);
        myTitleBar.setActionBar_center(R.string.more_setting_new_brainpower);
        myTitleBar.setActioBar_left(false, 0, R.string.dialog_cancel);
        myTitleBar.setActionBar_right(true, R.string.action_save);

        actionbar_left_tv = (TextView) findViewById(R.id.actionbar_tv_left);
        actionbar_left_tv.setTextColor(getResources().getColor(R.color.black_standard));
        actionbar_right_tv = (TextView) findViewById(R.id.actionbar_tv_right);
        actionbar_right_tv.setTextColor(getResources().getColor(R.color.electric_histogram));


        mTv_one = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_one);
        mTv_room = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_room);
        mTv_room_ll = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_room_ll);
        mIv_room_ll = (ImageView) findViewById(R.id.more_setting_new_brainpower_iv_select_room_ll);
        mTv_room_ll_no_select = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_non_select_room_ll);
        mRl_room = (RelativeLayout) findViewById(R.id.more_setting_new_brainpower_rl_room);

        mTv_two = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_two);
        mTv_condition = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_condition);
        mTv_condition_ll = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_condition_ll);
        mIv_condition_ll = (ImageView) findViewById(R.id.more_setting_new_brainpower_iv_select_condition_ll);
        mTv_condition_ll_no_select = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_non_select_condition_ll);
        mRl_condition = (RelativeLayout) findViewById(R.id.more_setting_new_brainpower_rl_condition);

        mTv_three = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_three);
        mTv_task = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_task);
        mTv_task_ll = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_select_task_ll);
        mIv_task_ll = (ImageView) findViewById(R.id.more_setting_new_brainpower_iv_select_task_ll);
        mTv_task_ll_no_select = (TextView) findViewById(R.id.more_setting_new_brainpower_tv_non_select_task_ll);
        mRl_task = (RelativeLayout) findViewById(R.id.more_setting_new_brainpower_rl_task);


        mRl_room.setOnClickListener(this);
        mRl_condition.setOnClickListener(this);
        mRl_task.setOnClickListener(this);


        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString(MoreSettingBrainpower.BRAINPOWER);
        if (name.equals(MoreSettingBrainpower.BRAINPOWER_NEW)) {

            actionbar_right_tv.setAlpha(0.25f);
            actionbar_right_tv.setClickable(false);
            mRl_condition.setClickable(false);
            mRl_task.setClickable(false);
        } else if (name.equals(MoreSettingBrainpower.BRAINPOWER_MODIFICATION)) {
            mTv_room_ll_no_select.setVisibility(View.GONE);
            mTv_room_ll.setVisibility(View.VISIBLE);
            mIv_room_ll.setVisibility(View.VISIBLE);
            mTv_one.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));

            mRl_condition.setClickable(true);
            mTv_condition.setTextColor(getResources().getColor(R.color.black_standard));


            mTv_condition_ll_no_select.setVisibility(View.GONE);
            mTv_condition_ll.setVisibility(View.VISIBLE);
            mIv_condition_ll.setVisibility(View.VISIBLE);
            mTv_two.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));

            mRl_task.setClickable(true);
            mTv_task.setTextColor(getResources().getColor(R.color.black_standard));


            mTv_task_ll_no_select.setVisibility(View.GONE);
            mTv_task_ll.setVisibility(View.VISIBLE);
            mIv_task_ll.setVisibility(View.VISIBLE);
            mTv_three.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_activity_relativelayout:
                this.finish();
                break;
            case R.id.actionbar_tv_right:
                //保存
//                startActivity(new Intent(this, MoreSettingBrainpower.class));
                this.finish();
                break;

            case R.id.more_setting_new_brainpower_rl_room:
                //选择房间
                startActivity_ForResult(MoreSettingBrainpowerSelectRoom.class, REQUEST_ONE);
                break;
            case R.id.more_setting_new_brainpower_rl_condition:
                //设置启动条件
                startActivity_ForResult(MoreSettingBrainpowerStartCondition.class, REQUEST_TWO);
                break;
            case R.id.more_setting_new_brainpower_rl_task:
                //添加执行任务
                startActivity_ForResult(MoreSettingBrainpowerPerformTask.class, REQUEST_THREE);
                break;
            default:
                break;
        }
    }

    private void startActivity_ForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent(MoreSettingNewBrainpower.this, cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 返回的时候调用
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //判断是由那个选项返回
        switch (requestCode) {
            case REQUEST_ONE:
                //判断返回的结果
                switch (resultCode) {
                    case RESULT_SUCCESS:
                        mTv_room_ll_no_select.setVisibility(View.GONE);
                        mTv_room_ll.setVisibility(View.VISIBLE);
                        mIv_room_ll.setVisibility(View.VISIBLE);
                        mTv_one.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));

                        mRl_condition.setClickable(true);
                        mTv_condition.setTextColor(getResources().getColor(R.color.black_standard));
                        break;
                    case RESULT_FAILED:

                        break;
                }
                break;
            case REQUEST_TWO:
                switch (resultCode) {
                    case RESULT_SUCCESS:
                        mTv_condition_ll_no_select.setVisibility(View.GONE);
                        mTv_condition_ll.setVisibility(View.VISIBLE);
                        mIv_condition_ll.setVisibility(View.VISIBLE);
                        mTv_two.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));

                        mRl_task.setClickable(true);
                        mTv_task.setTextColor(getResources().getColor(R.color.black_standard));
                        break;
                    case RESULT_FAILED:

                        break;
                }
                break;
            case REQUEST_THREE:
                switch (resultCode) {
                    case RESULT_SUCCESS:
                        mTv_task_ll_no_select.setVisibility(View.GONE);
                        mTv_task_ll.setVisibility(View.VISIBLE);
                        mIv_task_ll.setVisibility(View.VISIBLE);
                        mTv_three.setBackgroundColor(getResources().getColor(R.color.text_blue_normal));

                        actionbar_right_tv.setAlpha(1.00f);
                        actionbar_right_tv.setClickable(true);
                        break;
                    case RESULT_FAILED:

                        break;
                }
                break;
            default:
                break;
        }
    }
}
